<template>
  <e-table
    ref="eTable"
    :tableData="deptData"
    :columns="deptColumns"
    :loading="loading"
    :pageObj="null"
  >
    <el-table-column label="数据对应部门" slot="dataDept" align="center">
      <template slot-scope="scope">
        <dept-select
          ref="deptSelect"
          :selectValue="scope.row.dataDeptId"
          v-model="scope.row.dataDept"
          @select="e => handleDeptSelect(e, scope.row, 'dataDept')"
        />
      </template>
    </el-table-column>
    <el-table-column label="通知部门" slot="noticeDept" align="center">
      <template slot-scope="scope">
        <dept-select
          ref="deptSelect"
          :selectValue="scope.row.notifierDeptId"
          v-model="scope.row.dataDept"
          @select="e => handleDeptSelect(e, scope.row, 'noticeDept')"
        />
      </template>
    </el-table-column>
    <el-table-column label="通知岗位" slot="noticePosition" align="center">
      <template slot-scope="scope">
        <dept-position-s
          :selectValue="scope.row.notifierPositionId"
          @select="e => handlePositionSelect(e, scope.row)"
          ref="deptPositionS"
        />
      </template>
    </el-table-column>
    <el-table-column label="部门阀值" slot="threshold" align="center">
      <template slot-scope="scope">
        <el-input type="number" :min="0" v-model="scope.row.thresholdValue" placeholder="请输入部门阀值" />
      </template>
    </el-table-column>
    <el-table-column label="阀值说明" slot="explain" align="center">
      <template slot-scope="scope">
        <el-input v-model="scope.row.thresholdRemark" placeholder="请输入阀值说明" />
      </template>
    </el-table-column>
    <el-table-column slot="action" fixed="right" width="50" align="center">
      <template slot-scope="scope">
        <el-dropdown>
          <a class="el-dropdown-link">
            <i class="el-icon-menu"></i>
          </a>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item @click.native="handleDelete(scope.row)">
              <a class="red">删除</a>
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </template>
    </el-table-column>
  </e-table>
</template>

<script>
// 部门通知人表格组件，引用部门下拉和部门岗位下拉

import DeptSelect from '@/components/DeptSelect'
import DeptPositionS from '@/components/DeptPositionS'

export default {
  components: {
    DeptSelect,
    DeptPositionS
  },
  props: {
    tableData: {
      type: Array,
      default: []
    }
  },
  watch: {
    tableData: {
      handler(n, o) {
        this.deptData = [...n]
      }
    }
  },
  data() {
    return {
      loading: false,
      deptData: [],
      deptColumns: [{ slot: 'dataDept' }, { slot: 'noticeDept' }, { slot: 'noticePosition' }, { slot: 'threshold' }, { slot: 'explain' }, { slot: 'action' }]
    }
  },
  methods: {
    handleDelete(row) {
      const _this = this
      this.$confirm({
        title: '删除操作',
        content: `您确定删除这条记录吗？`,
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        centered: true,
        async onOk() {
          // 调用删除接口，刷新表格数据
          const index = _this.fieldData.findIndex(v => v.xmId === row.xmId)
          _this.fieldData.splice(index, 1)
          openNotification('success', '删除成功')
          _this.$emit('tableUpdate', _this.fieldData)
        }
      })
    },
    handleDeptSelect(value, row, type) {
      console.log('value', value, type)
      const index = this.deptData.findIndex(v => v.xmId === row.xmId)
      if (type === 'dataDept') {
        this.$set(this.deptData[index], 'dataDeptId', value.id)
      } else {
        this.$set(this.deptData[index], 'notifierDeptId', value.id)
      }
      this.$emit('tableUpdate', this.deptData)
    },
    handlePositionSelect(value, row) {
      console.log('value', value, row)
      const index = this.deptData.findIndex(v => v.xmId === row.xmId)
      this.$set(this.deptData[index], 'notifierPositionId', value.id)
      this.$emit('tableUpdate', this.deptData)
    }
  },
  created() {
    this.deptData = this.tableData
  }
}
</script>

<style scoped>
.data-table >>> .cell,
.data-table >>> .el-table,
.data-table >>> .el-table__body-wrapper {
  overflow: inherit;
}
</style>
